<?php

/**
 * calendar actions.
 *
 * @package    sfwixet
 * @subpackage calendar
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class calendarActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	try{
  		if(($start = $request->getParameter("start") && ($end = $request->getParameter("end")))){
  			
			echo json_encode(array("error"=>false,"tasks"=>$this->getUser()->getTaskList($start,$end)->toArray(),"birthdays"=>array()));
  		}else throw new Exception(_("Please provide a start and end date"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
    return sfView::NONE;
  }
  
  public function executeNewTask(sfWebRequest $request)
  {
  	try{
  		if(($start = $request->getParameter("start") && ($end = $request->getParameter("end")))){
  			$task = new CalendarTask();
  			$task->setUserId($this->getUser()->getId());
  			$task->setStart($request->getParameter("start"));
  			$task->setEnd($request->getParameter("end"));
  			$task->setTitle($request->getParameter("title"));
  			$task->setAllDay($request->getParameter("allDay"));
  			$task->save();
  		echo json_encode(array("error"=>false,"id"=>$task->getTaskId()));
  		}else throw new Exception(_("Please provide a start, an end date and a task title"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
    return sfView::NONE;
  }
  
  public function executeChangeTaskTitle(sfWebRequest $request)
  {
  	try{
		if(($request->getParameter("id") && ($title = $request->getParameter("title")))){
  			$task = Doctrine::getTable("CalendarTask")->find($request->getParameter("id"));
  			if($task->getUser()->getId() == $this->getUser()->getId()){
  				$task->setTitle($title);
  				$task->save();
  				echo json_encode(array("error"=>false));
  			}else throw new Exception(_("Permission denied"));
  		}else throw new Exception(_("Please provide the task id and new task title"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
    return sfView::NONE;
  }
  
  public function executeUpdateTask(sfWebRequest $request)
  {
  	try{
		if($request->getParameter("id")){
  			$task = Doctrine::getTable("CalendarTask")->find($request->getParameter("id"));
  			if($task->getUser()->getId() == $this->getUser()->getId()){
  				if($request->getParameter("title"))
  					$task->setTitle($request->getParameter("title"));
  				if($request->getParameter("start"))
  					$task->setStart($request->getParameter("start"));
  					
  			    if($request->getParameter("end"))
  			    	$task->setEnd($request->getParameter("end"));
  			    if($request->getParameter("allDay"))
  			    	$task->setAllDay($request->getParameter("allDay"));
  				$task->save();
  				echo json_encode(array("error"=>false));
  			}else throw new Exception(_("Permission denied"));
  		}else throw new Exception(_("Please provide the task id"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
    return sfView::NONE;
  }
  
  public function executeRemoveTask(sfWebRequest $request)
  {
  	try{
		if($request->getParameter("id")){
  			$task = Doctrine::getTable("CalendarTask")->find($request->getParameter("id"));
  			if($task->getUser()->getId() == $this->getUser()->getId()){
  				$task->delete();
  				echo json_encode(array("error"=>false));
  			}else throw new Exception(_("Permission denied"));
  		}else throw new Exception(_("Please provide the task id"));
  	}catch(Exception $e){
  		echo json_encode(array("error"=>true,"message"=>$e->getMessage()));
  	}
    return sfView::NONE;
  }
}
